﻿/**common function**/
String.prototype.replaceAll = function(s1, s2) {
    return this.replace(new RegExp(s1, "g"), s2);
}
var request = function(strname) {
    var hrefstr, pos, parastr, para, tempstr;
    hrefstr = window.location.href;
    pos = hrefstr.indexOf("?")
    parastr = hrefstr.substring(pos + 1);

    para = parastr.split("&");
    tempstr = "";
    for (i = 0; i < para.length; i++) {
        tempstr = para[i];
        pos = tempstr.indexOf("=");
        if (tempstr.substring(0, pos) == strname) {
            return tempstr.substring(pos + 1);
        }
    }
    return null;
}
function Regular() {
    this.isBlog = function(s) {
        var patrn = /^[a-z]([a-z]|[0-9]){2,38}$/;
        if (!patrn.exec(s))
            return false;
        return true;
    }
    this.isEmpty = function(str) {
        if (str != undefined) {
            if (str.length > 0) { return false; }
            else { return true; }
        } else { return true; }
    }
    this.isString = function(s) {
        var patrn = /^[a-z]{2,19}$/;
        if (!patrn.exec(s))
            return false;
        return true;
    }
    this.isEmail = function(str) {
        if (str == "undefined" || str.length == 0) {
            return false;
        } else {
            if (str.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) {
                return true;
            } else {
                return false;
            }
        }
    }
    this.isMobile = function(str) {
        if (str.search(/^(13|15|18)[0-9]{9}$/) != -1) {
            return true;
        } else {
            return false;
        }
    }
    this.isZipcode = function(str) {
        if (str.search(/^[0-9]{6}/) != -1) {
            return true;
        } else {
            return false;
        }
    }
    this.isNumber = function(str) {
        if (str.search(/^[0-9]/) != -1) {
            return true;
        } else {
            return false;
        }
    }
    this.isBirthday = function(str) {
        if (str.search(/^(19[0-9]{2})|200[0-9]-(0[0-9])|(1[1,2])-([0,1,2][0-9])|(0[0,1])$/) != -1) {
            return true;
        } else {
            return false;
        }
    }
}
/**end of common setting**/

/**global setting**/
var HandlerRoot = "../WebHandlers/";
/**end global setting**/


/**权限管理模型**/
var AuthModel = function () { }
AuthModel.initLogin = function () {
    $(".login").click(function () {
        AuthModel.loginAction();
    });
    $(".cancel").click(function () {
        AuthModel.cancelLogin();
    });
    $("#username").keypress(function (event) {
        if (event.keyCode == 13) {
            $("#password").focus();
        }
    });
    $("#password").keypress(function (event) {
        if (event.keyCode == 13) {
            AuthModel.loginAction();
        }
    });
    if (top.location != location) {
        top.location = location;
    }
}
AuthModel.checkLogin = function (username, password) {
    var reg = new Regular();
    if (reg.isEmpty(username)) {
        alert("Please input username!");
        return false;
    } else if (reg.isEmpty(password)) {
        alert("Please input password!");
        return false;
    } else if (!reg.isBlog(username)) {
        alert("Username only include charset and number!");
        return false;
    }
    return true;
}
AuthModel.cancelLogin = function () {
    $("#username").val("");
    $("#password").val("");
}
AuthModel.loginAction = function () {
    var username = $.trim($("#username").val());
    var password = $.trim($("#password").val());
    if (AuthModel.checkLogin(username, password)) {
        $(".login").attr("disabled", "disabled");
        $.ajax({
            url: HandlerRoot + "user/Login.ashx",
            data: { username: username, password: password, dt: new Date() },
            dataType: "json",
            type: "POST",
            success: function (json) {
                $(".login").removeAttr("disabled");
                alert(json.message);
                if (json.flag == "1") {
                    location.href = "../main/index.html";
                }
            },
            error: function () {
                alert("file load error!");
                $(".login").removeAttr("disabled");
            }
        });
    }
}
AuthModel.initNav = function () {
    $("#nav li a").each(function () {
        $(this).click(function () {
            $("#nav .current").removeClass("current");
            $(this).addClass("current");
        });
    });
}
AuthModel.initMenu = function () {
    $("#menu li a").each(function () {
        $(this).click(function () {
            $("#menu .current").removeClass("current");
            $(this).addClass("current");
        });
    });
}



/**init function**/
Init = function () { }
Init.redirect = function (url, seconds) {
    setTimeout("self.location='" + url + "';", seconds * 1000);
}
Init.tableList = function (o) {
    $(o + " tr:even td").addClass("item");
    $(o + " tr:odd td").addClass("aitem");
}
Init.deleleAction = function () {
    $("a.delete").each(function () {
        $(this).click(function () {
            return confirm("你确定要删除吗？这是不可逆的操作");
        });
    });
}